package com.onlineshop.highgee.daoimpl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;



import com.onlineshop.highgee.dao.MyOrderDao;
import com.onlineshop.highgee.entity.MyOrder;
import com.onlineshop.highgee.exception.MyOrderDaoException;




/**
 * 
 * @author 932080531
 * @date 2019骞�12鏈�23鏃� 涓嬪崍7:42:34
 * @version 1.0
 */
public class MyOrderDaoImpl implements MyOrderDao {
	/**
	 * 娣诲姞璁㈠崟淇℃伅
	 * @return
	 */
	@Override
	public void addMyOrder(MyOrder myOrder) throws MyOrderDaoException {
		// TODO Auto-generated method stub
		Connection con;
		PreparedStatement pre;
		String sql = "insert into my_order (order_number,order_adress,order_total,order_state,order_date,order_phone,order_name,order_contact) values(?,?,?,?,?,?,?,?)";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String uri = "jdbc:mysql://127.0.0.1:3306/highgee?user=root&password=&characterEncoding=utf-8&serverTimezone=CTT";
			con = DriverManager.getConnection(uri);
			pre = con.prepareStatement(sql);
			pre.setLong(1, myOrder.getOrder_number());
			pre.setString(2, myOrder.getOrder_adress());
			pre.setLong(3, myOrder.getOrder_total());
			pre.setString(4, myOrder.getOrder_state());
			pre.setDate(5, (Date) myOrder.getOrderDate());
			pre.setString(6, myOrder.getOrder_phone());
			pre.setString(7, myOrder.getOrder_name());
			pre.setString(8, myOrder.getOrder_contact());
			pre.executeUpdate();
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 鏌ヨ鎵�鏈夎鍗�(鎵�鏈夌敤鎴风殑璁㈠崟)
	 * @return List
	 * 
	 */
	@Override
	public List<MyOrder> findAll() throws MyOrderDaoException {
		MyOrder myOrder;
		int i = 0;
		List<MyOrder> list = new ArrayList<MyOrder>();
		Connection con;
		PreparedStatement pre;
		ResultSet rs;
		String sql = "select * from my_order";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String uri = "jdbc:mysql://127.0.0.1:3306/highgee?user=root&password=&characterEncoding=utf-8";
			con = DriverManager.getConnection(uri);
			pre = con.prepareStatement(sql);
			rs = pre.executeQuery();
			while (rs.next()) {
				myOrder = new MyOrder();
				myOrder.setOrder_number(rs.getLong(2));
				list.add(i, myOrder);
				i++;
			}
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * 鏌ヨ褰撳墠鐢ㄦ埛涓嬫墍鏈夌殑璁㈠崟
	 * @return List
	 * 
	 * 
	 */
	@Override
	public List<MyOrder> findByUserName(String userName) throws MyOrderDaoException {
		MyOrder myOrder;
		int i = 0;
		List<MyOrder> list = new ArrayList<MyOrder>();
		Connection con;
		PreparedStatement pre;
		ResultSet rs;
		String sql = "select * from my_order where order_name=?";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String uri = "jdbc:mysql://127.0.0.1:3306/highgee?user=root&password=&characterEncoding=utf-8";
			con = DriverManager.getConnection(uri);
			pre = con.prepareStatement(sql);
			pre.setString(1, userName);
			rs = pre.executeQuery();
			while (rs.next()) {
				myOrder = new MyOrder();
				myOrder.setOrder_number(rs.getLong(2));
				myOrder.setOrder_adress(rs.getString(3));
				myOrder.setOrder_total(rs.getInt(4));
				myOrder.setOrder_phone(rs.getString(7));
				myOrder.setOrder_name(rs.getString(8));
				list.add(i, myOrder);
				i++;
			}
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * 鏄剧ず褰撳墠璁㈠崟鐨勮缁嗕俊鎭�(杈撳叆璁㈠崟鍙�)
	 * @return MyOrder
	 * 
	 * 
	 */
//	@Test
	@Override
	public MyOrder findById(String id) throws MyOrderDaoException {
		MyOrder myOrder = null;
		List<MyOrder> list = new ArrayList<MyOrder>();
		Connection con;
		PreparedStatement pre;
		ResultSet rs;
		String sql = "select * from my_order where order_number=?";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String uri = "jdbc:mysql://127.0.0.1:3306/highgee?user=root&password=&characterEncoding=utf-8";
			con = DriverManager.getConnection(uri);
			pre = con.prepareStatement(sql);
			pre.setLong(1, Long.parseLong(id));
			rs = pre.executeQuery();
			while (rs.next()) {
				myOrder = new MyOrder();
				myOrder.setOrder_number(rs.getLong(2));
				myOrder.setOrder_adress(rs.getString(3));
				myOrder.setOrder_total(rs.getLong(4));
				myOrder.setOrder_state(rs.getString(5));
				myOrder.setOrderDate(rs.getDate(6));
				myOrder.setOrder_phone(rs.getString(7));
				myOrder.setOrder_name(rs.getString(8));
				myOrder.setOrder_contact(rs.getString(9));
			}
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return myOrder;
	}
	
	/**
	 * 鍒犻櫎璁㈠崟(杈撳叆璁㈠崟鍙�)
	 * @return
	 */
	@Override
	public void deleteMyOrder(String id) {
		// TODO Auto-generated method stub
		Connection con;
		PreparedStatement pre;
		String sql = "delete from my_order where order_number=?";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String uri = "jdbc:mysql://127.0.0.1:3306/highgee?user=root&password=&characterEncoding=utf-8&serverTimezone=CTT";
			con = DriverManager.getConnection(uri);
			pre = con.prepareStatement(sql);
			pre.setLong(1, Long.parseLong(id));
			pre.executeUpdate();
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
